package com.rongshu.view.userInfo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.discovery.bean.ArticleListItemBean;
import com.rongshu.view.userInfo.listener.OnShareFunctionClickListener;
import com.rongshu.widget.ArticleLabelView;
import com.widget.SwipeMenuView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShareAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext = null;
    private List<ArticleListItemBean> shareInfos = null;
    //interface
    private OnShareFunctionClickListener listener = null;
    //isVoice
    private boolean isVoice;

    public ShareAdapter(Context context, boolean isVoice) {
        this.mContext = context;
        this.isVoice = isVoice;
    }

    public void setShareInfos(List<ArticleListItemBean> list) {
        this.shareInfos = list;
    }

    public void addShareInfos(List<ArticleListItemBean> list) {
        this.shareInfos.addAll(list);
    }

    public List<ArticleListItemBean> getShareInfos() {
        return this.shareInfos;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_share_item_view, parent, false);
        return new ShareViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ArticleListItemBean responseBean = this.shareInfos.get(position);
        final ShareViewHolder holder = (ShareViewHolder) viewHolder;
        //文章头图
        holder.articleImg.setVisibility(TextUtils.isEmpty(responseBean.getTopImg()) ? View.GONE : View.VISIBLE);
        GlideUtils.loadImageView(mContext, holder.articleImg, responseBean.getTopImg(),
                R.drawable.default_preview_icon, R.drawable.default_preview_icon);
        //title
        holder.articleTitle.setText(responseBean.getTitle());
        //content
        holder.articleContent.setText(responseBean.getContents_header());
        //author
        holder.article_author.setVisibility(isVoice ? View.GONE : View.VISIBLE);
        holder.article_author.setText(responseBean.getBi_name());
        //类型
        holder.articleCategory.setText(responseBean.getCategories());
        //标签
        holder.articleLabel.bindDatas(responseBean.getKeywordList());
        //listener
        holder.contentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(position);
                }
            }
        });
        //delete
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDeleteClick(position);
                    holder.swipemenu_view.quickClose();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return shareInfos == null ? 0 : shareInfos.size();
    }

    public class ShareViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.article_img)
        ImageView articleImg;
        @BindView(R.id.article_title)
        TextView articleTitle;
        @BindView(R.id.article_content)
        TextView articleContent;
        @BindView(R.id.article_author)
        TextView article_author;
        @BindView(R.id.article_category)
        TextView articleCategory;
        @BindView(R.id.article_label)
        ArticleLabelView articleLabel;
        @BindView(R.id.btn_delete)
        TextView btn_delete;
        @BindView(R.id.swipemenu_view)
        SwipeMenuView swipemenu_view;
        @BindView(R.id.content_layout)
        LinearLayout contentLayout;

        public ShareViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemFunctionClickListener(OnShareFunctionClickListener listener) {
        this.listener = listener;
    }
}
